﻿using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Castle.Core.Logging;
using DevM.Development.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DevM.Development.DynamicInfomations
{
    public class DynamicInformationManager : ITransientDependency, IDomainService
    {
        private readonly IRepository<DynamicInfomation, long> dynamicInfomationRepository;
        private readonly IRepository<InformationResource, long> informationResourceRepository;
        private readonly ILogger logger;
        private readonly ResourceManager resourceManager;

        public DynamicInformationManager(IRepository<DynamicInfomation, long> dynamicInfomationRepository, IRepository<InformationResource, long> informationResourceRepository, ILogger logger, ResourceManager resourceManager)
        {
            this.dynamicInfomationRepository = dynamicInfomationRepository;
            this.informationResourceRepository = informationResourceRepository;
            this.logger = logger;
            this.resourceManager = resourceManager;
        }

        /// <summary>
        /// 动态列表
        /// </summary>
        public IQueryable<DynamicInfomation> DynamicInfomations { get => dynamicInfomationRepository.GetAll(); }
        public IQueryable<InformationResource> InformationResources { get => informationResourceRepository.GetAll(); }

        /// <summary>
        /// 添加动态
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        public async Task<DynamicInfomation> AddDynamicInfomation( DynamicInfomation dynamicInfomation)
        {
            dynamicInfomation.CreationTime = DateTime.Now;
            return await dynamicInfomationRepository.GetAsync(await dynamicInfomationRepository.InsertAndGetIdAsync(dynamicInfomation));
        }
        /// <summary>
        /// 更新动态信息
        /// </summary>
        /// <param name="modifierUserId"></param>
        /// <param name="project"></param>
        /// <returns></returns>
        public async Task<DynamicInfomation> UpdateDynamicInfomation(DynamicInfomation dynamicInfomation)
        {
            dynamicInfomation.LastModificationTime = DateTime.Now;
            return await dynamicInfomationRepository.UpdateAsync(dynamicInfomation);
        }
        /// <summary>
        /// 删除动态
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public async Task DeleteDynamicInfomation(long deleterUserId, params long[] dynamicInfomationId)
        {
            foreach (var id in dynamicInfomationId)
            {
                var project = await dynamicInfomationRepository.GetAsync(id);
                if (project != null)
                {
                    project.DeleterUserId = deleterUserId;
                    project.DeletionTime = DateTime.Now;
                    project.IsDeleted = true;
                    await dynamicInfomationRepository.UpdateAsync(project);

                }
            }
        }


        /// <summary>
        /// 添加资料
        /// </summary>
        /// <param name="environment"></param>
        /// <returns></returns>
        public async Task<List<InformationResource>> AddInformationResource(List<InformationResource> informationResources)
        {
            var res = new List<InformationResource>();
            foreach (var informationResource in informationResources)
            {
                informationResource.CreationTime = DateTime.Now;
                res.Add(await informationResourceRepository.InsertAsync(informationResource));
            }
            return res;
        }
        /// <summary>
        /// 删除资料
        /// </summary>
        /// <param name="deleterUserId"></param>
        /// <param name="environmentId"></param>
        /// <returns></returns>
        public async Task DeleteInformationResource(params long[] dynamicInfomationId)
        {
            var prs = await informationResourceRepository.GetAllListAsync(pr => dynamicInfomationId.Contains(pr.Id));
            await informationResourceRepository.DeleteAsync(pr => dynamicInfomationId.Contains(pr.Id));
            await resourceManager.DeleteResource(prs.Select(p => p.ResourceId).ToArray());
        }
        /// <summary>
        /// 根据动态获取资料
        /// </summary>
        /// <param name="dynamicInfomationId"></param>
        /// <returns></returns>
        public async Task<List<InformationResource>> GetInformationResourcesByInformationId(long dynamicInfomationId)
        {
            return await informationResourceRepository.GetAllListAsync(en => en.DynamicInfomationId == dynamicInfomationId);
        }

    }
}
